import unittest,time
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.support.select import Select

# 定义类
class JiuXian(unittest.TestCase):
    # TestFixture测试固件
    def setUp(self):
        # 准备环境
        self.driver=webdriver.Edge()
        self.driver.get('https://www.jiuxian.com/')
        self.driver.maximize_window()

    def tearDown(self):
        # 清理环境
        time.sleep(10)
        self.driver.quit()

    # TestCase 测试用例
    # 登录酒仙网 账号：15253006116 密码：Aa123456
    def test_01_login(self):
        # 1 酒仙网登录成功 class="headerLogin"
        self.driver.find_element(By.CLASS_NAME,'headerLogin').click()
        # 2 输入账号 id="userName"
        self.driver.find_element(By.ID, 'userName').send_keys('18986670716')
        # 3 输入密码 id="password"
        self.driver.find_element(By.ID, 'password').send_keys('Aa123456')
        # 4 输入验证码 强等10s
        time.sleep(15)
        # 5 点击登录按钮 name="bi_Usuallogin" and id="save"
        self.driver.find_element(By.XPATH, '//input[@name="bi_Usuallogin" and @id="save"]').click()
        # 6 点击确认 class="bindSub" or id="bindSub"
        self.driver.find_element(By.XPATH, '//a[@class="bindSub" or @id="bindSub"]').click()

        # 调用成功登录酒仙网
        # 成功添加收货地址
    def test_02_address(self):
        # 1调用业务1登录成功
        self.test_01_login()
        # 2 点击 我的酒仙 a href="https://member.jiuxian.com/index.htm"
        self.driver.find_element(By.XPATH, '//a[@href="https://member.jiuxian.com/index.htm"]').click()
        # 多窗口切换
        self.driver.switch_to.window(self.driver.window_handles[-1])

        time.sleep(2)
        # 点击关闭弹窗
        self.driver.find_element(By.CLASS_NAME, 'uGuideClose').click()
        time.sleep(2)
        # 向下滚动800像素-1000--1200-1300
        self.driver.execute_script('window.scrollTo(0,800)')
        self.driver.execute_script('window.scrollTo(0,document.body.scrollHight)')
        # 点击地址
        self.driver.find_element(By.XPATH, ' /html/body/div[4]/div[1]/div[2]/div[4]/a[3]').click()
        # 4点击添加新地址class="addressTrue"
        self.driver.find_element(By.CLASS_NAME, 'addressTrue').click()
        time.sleep(2)
        # 5 输入收货人id="people"
        self.driver.find_element(By.ID, 'people').send_keys('RW')
        # 6下拉菜单选择地区
        # 选择地区
        # province = Select(self.driver.find_element(By.ID, 'province'))
        # province.select_by_value('2')
        province=Select(self.driver.find_element(By.ID,'province'))
        province.select_by_value('2')
        time.sleep(1)
        city = Select(self.driver.find_element(By.ID, 'city'))
        city.select_by_index(1)
        time.sleep(1)
        district = Select(self.driver.find_element(By.ID, 'district'))
        # district.select_by_visible_text("昌平区")
        district.select_by_visible_text('昌平区')
        time.sleep(1)
        # 7输入详细地址
        self.driver.find_element(By.ID, 'address').send_keys('南口南大街')
        # 8输入邮编
        # 9输入手机
        self.driver.find_element(By.ID, 'iphone').send_keys('18986670716')
        # 点击添加
        self.driver.find_element(By.CLASS_NAME, 'qjwSubmit2').click()
        time.sleep(5)
        # 10 点击确定
        self.driver.find_element(By.CLASS_NAME, 'confirm.alertSure').click()

    def test_03_delete_address(self):
        # 连接登录
        self.test_01_login()
        #       点击我的酒仙
        # 2 点击 我的酒仙 a href="https://member.jiuxian.com/index.htm"
        self.driver.find_element(By.XPATH, '//a[@href="https://member.jiuxian.com/index.htm"]').click()
        # 多窗口切换
        self.driver.switch_to.window(self.driver.window_handles[-1])
        time.sleep(2)
        # 点击关闭弹窗
        self.driver.find_element(By.CLASS_NAME, 'uGuideClose').click()
        time.sleep(2)
        # 向下滚动800像素-1000--1200-1300
        self.driver.execute_script('window.scrollTo(0,800)')
        # 点击地址
        self.driver.find_element(By.XPATH, ' /html/body/div[4]/div[1]/div[2]/div[4]/a[3]').click()
        # 点击删除
        self.driver.find_element(By.CLASS_NAME, 'controlRemove').click()
        # 点击确定
        self.driver.find_element(By.CLASS_NAME, 'confirm.alertSure').click()
        time.sleep(2)
        self.driver.find_element(By.CLASS_NAME, 'confirm.alertSure').click()

if __name__ == '__main__':
    unittest.main()